/*
Copyright (C) 2008 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

var NORMAL_TEXT_COLOR = '#000000';
var GREEN_TEXT_COLOR = '#007000';
var RED_TEXT_COLOR = '#900000';

var URL_NEWS = 'http://finance.google.com/finance?morenews=10&rating=1&q=';
var URL_DISCLAIMER = 'http://www.google.com/intl/en/help/stock_disclaimer.html';

function getChangeColor(change) {
  if (change < 0) {
    return RED_TEXT_COLOR;
  } else if (change > 0) {
    return GREEN_TEXT_COLOR;
  } else {
    return NORMAL_TEXT_COLOR;
  }
}

function onOpen() {
  var security = detailsViewData.getValue('security');

  var changeColor = getChangeColor(security.change);

  _last.innerText = security.last;
  _change.innerText = security.change;
  _change.color = changeColor;
  _changePercent.innerText = security.changePercent + '%';
  _changePercent.color = changeColor;
  _lastTrade.innerText = security.lastDateTime;

  _open.innerText = security.open;
  _volume.innerText = Util.trimDecimalDigitsIfGe1000(security.volume);
  _high.innerText = security.high;
  _avgVol.innerText = Util.trimDecimalDigitsIfGe1000(security.averageVolume);
  _low.innerText = security.low;
  _mktCap.innerText = security.marketCap;
  _high52w.innerText = security.fiftyTwoWeekHigh;
  _exchange.innerText = security.getPrettyExchange();
  _low52w.innerText = security.fiftyTwoWeekLow;

  _recentNews.innerText = Util.substitutePH(strings.RECENT_NEWS,
      '[![TICKER]!]',
      security.fullticker);
  _recentNews.href = URL_NEWS + encodeURIComponent(security.ticker);
  _delay.innerText = security.getDelayedMessage();
  _disclaimer.href = URL_DISCLAIMER;

  if (security.isAfterHours()) {
    _afterHoursTable.visible = true;
    _footer.y = _afterHoursTable.y + _afterHoursTable.height;
    var afterChangeColor = getChangeColor(security.extendedChange);

    _afterLast.innerText = security.extendedLast;
    _afterChange.innerText = security.extendedChange;
    _afterChange.color = afterChangeColor;
    _afterChangePercent.innerText = security.extendedChangePercent + '%';
    _afterChangePercent.color = afterChangeColor;
    _afterLastTrade.innerText = security.extendedLastDateTime;
  }

  var chartUrl = ChartServer.getChartUrl(security);

  if (chartUrl) {
    Util.retrieveImage(_chart, chartUrl);
  } else {
    _noChart.visible = true;
  }
}
